package com.qimei.onerice.dao.impl;


import com.qimei.onerice.dao.OrderDetailDao;
import com.qimei.onerice.entity.OrderDetail;
import com.qimei.onerice.until.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDetailDaoImpl implements OrderDetailDao {

	private static final String INSERT_ORDER_DETAIL = "INSERT INTO order_details (order_detail_id, order_id, dish_id, quantity, unit_price, subtotal) VALUES (?, ?, ?, ?, ?, ?)";
	private static final String SELECT_ORDER_DETAIL_BY_ID = "SELECT * FROM order_details WHERE order_detail_id = ?";
	private static final String SELECT_ORDER_DETAILS_BY_ORDER_ID = "SELECT * FROM order_details WHERE order_id = ?";
	private static final String UPDATE_ORDER_DETAIL = "UPDATE order_details SET order_id = ?, dish_id = ?, quantity = ?, unit_price = ?, subtotal = ? WHERE order_detail_id = ?";
	private static final String UPDATE_QUANTITY = "UPDATE order_details SET quantity = ? subtotal = ? WHERE order_detail_id = ?";
	private static final String DELETE_ORDER_DETAIL = "DELETE FROM order_details WHERE order_detail_id = ?";
	private static final String GET_ORDER_DETAIL_BY_DISH = "SELECT * FROM order_details WHERE order_id = ? and dish_id = ?";
	@Override
	public int addOrderDetail(OrderDetail orderDetail) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_ORDER_DETAIL)) {
			preparedStatement.setInt(1, orderDetail.getOrderDetailId());
			preparedStatement.setInt(2, orderDetail.getOrderId());
			preparedStatement.setInt(3, orderDetail.getDishId());
			preparedStatement.setInt(4, orderDetail.getQuantity());
			preparedStatement.setDouble(5, orderDetail.getUnitPrice());
			preparedStatement.setDouble(6, orderDetail.getSubtotal());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public OrderDetail getOrderDetailById(int orderDetailId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ORDER_DETAIL_BY_ID)) {
			preparedStatement.setInt(1, orderDetailId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createOrderDetailFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public OrderDetail getOrderDetailByDishId(int orderId, int dishId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(GET_ORDER_DETAIL_BY_DISH)) {
			preparedStatement.setInt(1, orderId);
			preparedStatement.setInt(2, dishId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createOrderDetailFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<OrderDetail> getOrderDetailsByOrderId(int orderId) {
		List<OrderDetail> orderDetails = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ORDER_DETAILS_BY_ORDER_ID)) {
			preparedStatement.setInt(1, orderId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					orderDetails.add(createOrderDetailFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return orderDetails;
	}

	@Override
	public int updateOrderDetail(OrderDetail orderDetail) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ORDER_DETAIL)) {
			preparedStatement.setInt(1, orderDetail.getOrderId());
			preparedStatement.setInt(2, orderDetail.getDishId());
			preparedStatement.setInt(3, orderDetail.getQuantity());
			preparedStatement.setDouble(4, orderDetail.getUnitPrice());
			preparedStatement.setDouble(5, orderDetail.getSubtotal());
			preparedStatement.setInt(6, orderDetail.getOrderDetailId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int updateQuantity(int orderDetailId, int quantity) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ORDER_DETAIL)) {
			preparedStatement.setInt(1, orderDetailId);
			preparedStatement.setInt(2, quantity);
			preparedStatement.setInt(3, orderDetailId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteOrderDetail(int orderDetailId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ORDER_DETAIL)) {
			preparedStatement.setInt(1, orderDetailId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 OrderDetail 对象
	private OrderDetail createOrderDetailFromResultSet(ResultSet resultSet) throws SQLException {
		OrderDetail orderDetail = new OrderDetail();
		orderDetail.setOrderDetailId(resultSet.getInt("order_detail_id"));
		orderDetail.setOrderId(resultSet.getInt("order_id"));
		orderDetail.setDishId(resultSet.getInt("dish_id"));
		orderDetail.setQuantity(resultSet.getInt("quantity"));
		orderDetail.setUnitPrice(resultSet.getDouble("unit_price"));
		orderDetail.setSubtotal(resultSet.getDouble("subtotal"));
		return orderDetail;
	}
}